package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.repository.StrategyCommentRepository;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {

    @Autowired
    private MongoTemplate template;
    @Autowired
    private StrategyCommentRepository repository;

    @Override
    public void save(StrategyComment strategyComment) {
        strategyComment.setId(null);
        repository.save(strategyComment);
    }

    @Override
    public void update(StrategyComment strategyComment) {
        repository.save(strategyComment);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public StrategyComment get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<StrategyComment> list() {
        return repository.findAll();
    }



    @Override
    public void commentThumb(String cid, String uid) {
        StrategyComment comment = this.get(cid);
        List<String> thumbuplist = comment.getThumbuplist();
        if(thumbuplist.contains(uid)){
            //当点赞id list中有当前用户id时候,此时为取消点赞
            comment.setThumbupnum(comment.getThumbupnum()-1);
            thumbuplist.remove(uid);
        }else {
            //当点赞id list中没有当前用户id时候,此时为点赞
            comment.setThumbupnum(comment.getThumbupnum()+1);
            thumbuplist.add(uid);
        }
        this.update(comment);
    }

    @Override
    public void addComment(StrategyComment comment) {
        comment.setCreateTime(new Date());
        this.save(comment);
    }

    @Override
    public Page<StrategyComment> query(StrategyCommentQuery qo) {
        Query query = new Query();
        if(StringUtils.hasLength(qo.getStrategyId())){
            query.addCriteria(Criteria.where("strategyId").is(qo.getStrategyId()));
        }
        long count = template.count(query, StrategyComment.class);
        if (count == 0) {
            Page.empty();
        }
        if (count % qo.getPageSize() == 0 && qo.getCurrentPage() > 1) {
            qo.setCurrentPage(qo.getCurrentPage() - 1);
        }
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        query.with(pageable);
        List<StrategyComment> strategyComments = template.find(query, StrategyComment.class);
        return new PageImpl<>(strategyComments, pageable, count);
    }


}

